package iscte.ai.kcgtd;

import java.util.ArrayList;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.TextView;

public class TaskViewAdapter extends BaseAdapter{
	private Context cntxt;
	private int item_layout;
	private ArrayList<Task> tasks;
	private LayoutInflater inflater;

	public TaskViewAdapter(Context context, int item_layout, ArrayList<Task> tasks) {
		super();
		this.cntxt = context;
		this.item_layout = item_layout;
		this.tasks = tasks;
		inflater = (LayoutInflater)cntxt.getSystemService(Context.LAYOUT_INFLATER_SERVICE);

	}

	@Override
	public int getCount() {
		if(!tasks.isEmpty())
			return tasks.size();
		else
			return 1;
	}

	@Override
	public Object getItem(int position) {
		return tasks.get(position);
	}

	@Override
	public long getItemId(int arg0) {
		return 0;
	}

	@Override
	public View getView(int position, View view, ViewGroup arg2) {
		view = inflater.inflate(item_layout, null);
		TextView name = (TextView) view.findViewById(R.id.task_item_name);
		TextView date = (TextView) view.findViewById(R.id.task_item_date);
		
		if(!tasks.isEmpty()){	
			name.setText(tasks.get(position).getName());
			
			String dateStr = "";

			if(tasks.get(position).getDuration() != null){
				dateStr = "Duração: " + tasks.get(position).getDuration() + "h";
			}
			
			String locaitonStr = "";
			
			if(tasks.get(position).getLocation() != null){
				locaitonStr = ", Localização: " + tasks.get(position).getLocation();
			}
			
			date.setText(dateStr + locaitonStr);
		}else{
			name.setText("Sem Objectivos");
			date.setText("");
		}

		return view;
	}

}
